package com.blb.controller;


import com.blb.entity.Product;
import com.blb.entity.Sort;
import com.blb.entity.User;
import com.blb.service.IProductService;
import com.blb.utils.DataTablesPage;
import com.blb.utils.ResponseEntity;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *商品管理
 * @author wss
 * @since 2020-05-21
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService iProductService;
    @RequiresPermissions("productinfo")
    @GetMapping("products_info")
    public String products_info() {
        return "product/Products_info";
    }
    @RequiresPermissions("productinfo:add")
    @GetMapping("good_add")
    public String good_add() {
        return "product/good_add";
    }
    @RequiresPermissions("productinfo:update")
    @GetMapping("editor")
    public String editor() {
        return "product/editor";
    }
    @RequiresPermissions("productcheck")
    @GetMapping("products_check")
    public String products_check() {
        return "product/Products_check";
    }
    @RequiresPermissions("product")
    @ResponseBody
    @PostMapping("/upload")
    public ResponseEntity upload(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String subffix = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());

        String path = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\upload\\";

        String fileName = UUIDUtils.getId() + subffix;
        File f = new File(path + fileName);

        file.transferTo(f);
        return ResponseEntity.success(fileName);
    }
    @RequiresPermissions("productinfo:add")
    @ResponseBody
    @RequestMapping("/addProduct")
    public String addProduct(Product product) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        product.setCompanyId(user.getCompany().getId());
        product.setCreateTime(new Date());
        boolean a = iProductService.save(product);
        if (a == true) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }
    @RequiresPermissions("productinfo")
    @ResponseBody
    @RequestMapping("/productByIdss")
    public String productByIdss(String id) {
        int b = iProductService.productByIdss(id);
        if (b > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }
    @RequiresPermissions("productinfo")
    @RequestMapping("list")
    @ResponseBody
    public DataTablesPage getProductList(Integer draw, Integer start, Integer length, String productName, String status, Model model) {
        if (status == null || "".equals(status)) {
            status = "-1";
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int count = iProductService.getProductCount(productName, Integer.parseInt(status), user.getCompany().getId());
        List<Product> list = iProductService.getProductList(start, length, productName, Integer.parseInt(status), user.getCompany().getId());
        return new DataTablesPage(draw, count, count, list);
    }
    @RequiresPermissions("product")
    @ResponseBody
    @RequestMapping("states")
    public Map<String, Integer> states() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Map<String, Integer> map = new HashMap<>();
        int a = iProductService.states1(user.getCompany().getId());
        int b = iProductService.states2(user.getCompany().getId());
        int c = iProductService.states3(user.getCompany().getId());
        map.put("a", a);
        map.put("b", b);
        map.put("c", c);
        return map;
    }
    @RequiresPermissions("productinfo:update")
    @ResponseBody
    @RequestMapping("/productUpdate")
    public String productUpdate(Product product) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (product.getPicture() == null) {
            Product product1 = iProductService.productById(product.getId(), user.getCompany().getId());
            product.setPicture(product1.getPicture());
            boolean a = iProductService.updateById(product);
            if (a == true) {
                return "{\"result\":true}";
            } else {
                return "{\"result\":false}";
            }
        } else {
            Product product1 = iProductService.productById(product.getId(), user.getCompany().getId());
            String path = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\upload\\";
            String delpath = path + product1.getPicture();
            File f = new File(delpath);
            f.delete();
            boolean a = iProductService.updateById(product);
            if (a == true) {
                return "{\"result\":true}";
            } else {
                return "{\"result\":false}";
            }
        }
    }
    @RequiresPermissions("product")
    @RequestMapping("/productById")
    public String productById(String id, Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Product product = iProductService.productById(id, user.getCompany().getId());
        model.addAttribute("product", product);
        List<Sort> sort = iProductService.getSort(user.getCompany().getId());
        model.addAttribute("sort", sort);
        List<Sort> sorts = iProductService.getSorts(product.getSortId(), user.getCompany().getId());
        model.addAttribute("sorts", sorts);
        List<Sort> sortss = iProductService.getSortss(product.getSortsId(), user.getCompany().getId());
        model.addAttribute("sortss", sortss);
        return "product/editor";
    }
    @RequiresPermissions("product")
    @RequestMapping("/productByIds")
    public String productByIds(String id, Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Product product = iProductService.productByIds(id, user.getCompany().getId());
        model.addAttribute("product", product);
        return "product/Product";
    }
    @RequiresPermissions("product")
    @RequestMapping("/sort")
    @ResponseBody
    public List<Sort> productSort() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Sort> list1 = iProductService.getSort(user.getCompany().getId());
        return list1;
    }
    @RequiresPermissions("classfyied")
    @RequestMapping("/sorts")
    @ResponseBody
    public List<Sort> productSorts(String sortId) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String companyId = user.getCompany().getId();
        List<Sort> list2 = iProductService.getSorts(sortId, companyId);
        return list2;
    }
    @RequiresPermissions("classfyied")
    @RequestMapping("/sortss")
    @ResponseBody
    public List<Sort> productSortss(String sortId) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Sort> list3 = iProductService.getSortss(sortId, user.getCompany().getId());
        return list3;
    }
    @RequiresPermissions("product")
    @ResponseBody
    @RequestMapping("/productStates1")
    public String productStates1(String id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int a = iProductService.productStates1(id, user.getCompany().getId());
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":fasle}";
        }
    }
    @RequiresPermissions("product")
    @ResponseBody
    @RequestMapping("/productStates2")
    public String productStates2(String id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int a = iProductService.productStates2(id, user.getCompany().getId());
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":fasle}";
        }
    }
    @RequiresPermissions("product")
    @ResponseBody
    @RequestMapping("/examineStates1")
    public String examineStates1(String id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int a = iProductService.examineStates1(id, user.getCompany().getId());
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":fasle}";
        }
    }
    @RequiresPermissions("product")
    @ResponseBody
    @RequestMapping("/examineStates2")
    public String examineStates2(String id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int a = iProductService.examineStates2(id, user.getCompany().getId());
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":fasle}";
        }
    }
}

